# -*- coding: utf-8 -*-
__author__ = 'Junior Lima'
from django import forms
class BrDataWidget(forms.DateTimeInput, Masked):
    """
    Define o ``format`` e aplica a máscara javascript a partir da classe.
    """
    format = '%d/%m/%Y'

    def __init__(self, attrs=None, format=None, show_label=True):
        attrs = attrs or {}
        if show_label:
            attrs.update({'class': 'br-data-widget', 'size': '10', 'maxlength': '10'})
        else:
            attrs.update({'class': 'br-data-widget labeless', 'size': '10', 'maxlength': '10'})
        super(forms.DateTimeInput, self).__init__(attrs)
        if format:
            self.format = format

class BrDataField(forms.DateField):

    default_error_messages = {
        'max_value': _(u'Ensure this value is less than or equal to %s.'),
        'min_value': _(u'Ensure this value is greater than or equal to %s.'),
    }

    widget = BrDataWidget

    def __init__(self, input_formats=None, *args, **kwargs):
        self.min_value = kwargs.pop('min_value', None)
        self.max_value = kwargs.pop('max_value', None)
        super(forms.DateField, self).__init__(*args, **kwargs)
        self.input_formats = ('%d/%m/%Y',)

    def clean(self, value):
        value = super(BrDataField, self).clean(value)
        if value in EMPTY_VALUES:
            return None
        if self.max_value is not None and value > self.max_value:
            raise forms.ValidationError(self.error_messages['max_value'] % self.max_value.strftime('%d/%m/%Y'))
        if self.min_value is not None and value < self.min_value:
            raise forms.ValidationError(self.error_messages['min_value'] % self.min_value.strftime('%d/%m/%Y'))
        return value